package cn.muqingfeng.bbs.ui;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.muqingfeng.api.RequestHelper;
import cn.muqingfeng.api.UriHelper;
import cn.muqingfeng.api.request.GsonRequest;
import cn.muqingfeng.api.to.BaseResponseTO;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.bbs.constants.Constants;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;

public class FindPasswordAvtivity extends BaseActivity implements OnClickListener {
	private static final Object LOCK = new Object();
	private TextView mTipView;
	private EditText mEmailView;
	private Button mSubmitBtn;
	private boolean isSubmit = false;
	private ProgressDialog mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_pass);
		setupViews();
	}

	private void setupViews() {
		findViewById(R.id.back).setOnClickListener(this);
		mTipView = (TextView) findViewById(R.id.tip);
		mEmailView = (EditText) findViewById(R.id.email);
		mSubmitBtn = (Button) findViewById(R.id.submit);
		mSubmitBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.submit:
			submit();
			break;
		default:
			break;
		}
	}

	private void submit() {
		synchronized (LOCK) {
			if (isSubmit)
				return;
			final String email = mEmailView.getText().toString();
			if (TextUtils.isEmpty(email)) {
				Toast.makeText(getApplicationContext(), "请输入邮箱!", Toast.LENGTH_SHORT).show();
				return;
			}
			Matcher matcher = Constants.EMAIL_ADDRESS.matcher(email);
			if (!matcher.matches()) {
				Toast.makeText(getApplicationContext(), "请输入合法的邮箱!", Toast.LENGTH_SHORT).show();
				return;
			}
			showDialog();
			isSubmit = true;
			String requestUrl = UriHelper.gerFindPasswordUrl();
			HashMap<String, String> params = new HashMap<String, String>();
			params.put(UriHelper.PARAMS_EMAIL, email);
			GsonRequest<BaseResponseTO> request = new GsonRequest<BaseResponseTO>(Method.GET, requestUrl, new Listener<BaseResponseTO>() {

				@Override
				public void onResponse(BaseResponseTO response) {
					isSubmit = false;
					dismissDialog();
					if (!isDestroied()) {
						if (response == null) {
							Toast.makeText(getApplicationContext(), "请求失败，请重试", Toast.LENGTH_SHORT).show();
						} else {
							if (response.isSuccess()) {
								mTipView.setVisibility(View.VISIBLE);
								mTipView.setText(String.format("修改密码链接已经发送至“%s”，请在三天内登录该邮箱进行密码修改。", email));
								mTipView.setTextColor(getResources().getColor(R.color.color_find_pass_success));
							} else {
								mTipView.setVisibility(View.VISIBLE);
								mTipView.setText("抱歉！使用此Email的用户不存在，不能用于找回密码功能。");
								mTipView.setTextColor(getResources().getColor(R.color.color_find_pass_error));
							}
						}
					}
				}
			}, new ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					isSubmit = false;
					dismissDialog();
					Toast.makeText(getApplicationContext(), "请求失败，请重试", Toast.LENGTH_SHORT).show();
				}
			}, params, BaseResponseTO.class);
			RequestHelper.startRequest(getApplicationContext(), request);
		}
	}

	private void showDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			return;
		}
		mDialog = new ProgressDialog(this);
		mDialog.setMessage("正在请求，请稍等...");
		mDialog.show();
	}

	private void dismissDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}
}
